/**
 * @FileName: PromotionController.java
 * @Author
 * @Description:
 * @Date 2016年6月27日 下午2:31:12
 * @CopyRight ZTE Corporation
 */
package cn.hpclub.server.controller.admin.promotion;

import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.hpclub.server.controller.admin.BaseAdminController;
import cn.hpclub.server.model.Brand;
import cn.hpclub.server.model.Organization;
import cn.hpclub.server.model.product.Product;
import cn.hpclub.server.model.promotion.Promotion;
import cn.hpclub.server.service.BrandService;
import cn.hpclub.server.service.product.ProductService;
import cn.hpclub.server.service.product.ProductService.CheckBoxBean;
import cn.hpclub.server.service.promotion.PromotionService;
import cn.hpclub.server.util.DateKit;
import cn.hpclub.server.util.InterfaceUtil;

/**
 * @FileName: PromotionController.java
 * @Author zhl
 * @Description:首页弹窗信息Controller
 * @Date 2016年6月27日 下午2:38:57
 * @CopyRight ZTE Corporation
 */
public class PromotionController extends BaseAdminController<Promotion>{

    /**
     * 
     * @Title: add
     * @Description:新增一条首页弹窗数据页面初始化
     */
    public void add(){
        /* 获取城市列表 */
        List<CheckBoxBean> regionList = ProductService.service.getRegionList();
        setAttr("regionList", regionList);
        Record product = new Record();
        product.set("put_on_time", new Date()); // 默认显示今天
        setAttr("product", product);
        // 获取商品可选品牌信息
        List<Brand> brands = BrandService.service.getAllEnabledBrands();
        setAttr("brands", brands);
        render("/admin/promotion_input.tpl");
    }

    /**
     * 
     * @Title: add
     * @Description:修改一条首页弹窗数据页面初始化
     */
    public void edit(){
        String id = getPara("id", null);
        if(StrKit.notBlank(id)){
            Promotion promotion = Promotion.dao.findById(id);
            setAttr("promotion", promotion);
        }
        render("/admin/promotion_input.tpl");
    }

    /**
     * 
     * @Title: save
     * @Description:保存一条首页弹窗数据
     */
    public void save(){
        Promotion obj = new Promotion();
        String promotionId = getPara("promotionId", null);
        String name = getPara("name", null);
        String img = getPara("img", null);
        String url = getPara("url", null);
        String start_time = getPara("start_time", null);
        String end_time = getPara("end_time", null);
        String issuance = getPara("issuance", null);
        String creator = getPara("creator", null);
        if(StrKit.isBlank(creator)){
            creator = InterfaceUtil.getAdminId() + "";
        }
        int org_id = InterfaceUtil.getOrgId();
        // 获取系统当前时间
        String create_time = DateKit.toStr(new Date(), "yyyy-MM-dd HH:mm:ss");
        // 检查必填字段
        if(StrKit.isBlank(name)  || StrKit.isBlank(start_time) || StrKit.isBlank(end_time)
                || StrKit.isBlank(issuance) || StrKit.isBlank(creator)){
            throw new IllegalArgumentException("参数错误");
        }

        obj.set(Promotion.NAME, name);
        obj.set(Promotion.IMG, img);
        obj.set(Promotion.URL, url);
        obj.set(Promotion.START_TIME, start_time);
        obj.set(Promotion.END_TIME, end_time);
        obj.set(Promotion.ISSUANCE, issuance);
        obj.set(Promotion.LAST_MODIFY_ID, creator);
        obj.set(Promotion.LAST_MODIFY_TIME, create_time);
        obj.set(Promotion.ORG_ID, org_id);
        if(StrKit.notBlank(promotionId)){
            // 更新商品信息
            obj.set(Promotion.ID, promotionId);
            obj.update();
        } else{
            obj.set(Promotion.CREATOR, creator);// 新增时才更新创建人Id
            obj.set(Promotion.CREATE_TIME, create_time);
            if(obj.save()){
                promotionId = String.valueOf(obj.getInt(Product.ID));
            }
        }
        redirect("/promotion/list");
    }

    /**
     * 
     * @Title: list
     * @Description:分页获取首页弹窗数据列表
     */
    public void list(){
        int defaultOrgId = initOrgTreeWithDefaultName();
        ConcurrentHashMap<String, String> searchInfo = getSearchInfo();
        if(searchInfo.containsKey("issuance") && "all".equals(searchInfo.get("issuance"))){
            searchInfo.remove("issuance");
        }
        if(!searchInfo.containsKey("org_id") || "-1".equals(searchInfo.get("org_id"))){
            searchInfo.put("org_id", InterfaceUtil.getOrgId() + "");
        }
        findByPageForSearch("SELECT h.*,su.name as user_name  ",
                            new PromotionService().service.getSqlExceptSelect(searchInfo));
        render("/admin/promotion_list.tpl");
    }

    /**
     * 
     * @Title: deletePromo
     * @Description:通过主键Id删除一条首页弹窗数据
     */
    public void deletePromo(){
        String[] ids = getParaValues("ids");
        if(ids == null || ids.length <= 0){
            ajaxJsonErrorMessage("删除失败，无选中项");
        }

        boolean deleted = true;
        for(String id : ids){
            Promotion promotion = new Promotion();
            promotion.set(Promotion.ID, id);
            promotion.set(Promotion.ISDELETE, 1);
            promotion.set(Promotion.LAST_MODIFY_ID, InterfaceUtil.getAdminId());
            promotion.set(Promotion.LAST_MODIFY_TIME, DateKit.toStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
            if(!promotion.update()){
                deleted = false;
                break;
            }
        }

        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("code", 1000);
        if(deleted){
            jsonMap.put("message", "success");
        } else{
            jsonMap.put("message", "failed");
        }
        renderJson(jsonMap);

    }



}
